<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
    /**
    * Definition for singly-linked list.
    * function ListNode(val) {
    *     this.val = val;
    *     this.next = null;
    * }
    */
    /**
    * @param {ListNode[]} lists
    * @return {ListNode}
    */
    function ListNode(val) {
         this.val = val;
         this.next = null;
    }
    var mergeKLists = function(lists) {
        if(lists.length==0){
            return null;
        }
        else if(lists.length==1){
            return lists[0];
        }
        else if(lists.length==2){
            return mergeTwoLists(lists[0],lists[1]);
        }
        var mid=Math.floor(lists.length/2);
        var l1=lists.slice(0,mid);
        var l2=lists.slice(mid);
        return mergeTwoLists(mergeKLists(l1),mergeKLists(l2));
    };
    var mergeTwoLists=function(l1,l2){
        if(l1==null)
        return l2;
        if(l2==null)
        return l1;
        var newhead=null;
        if(l1.val<=l2.val)
        {
            newhead=l1;
            newhead.next=mergeTwoLists(l1.next,l2);
        }else{
            newhead=l2;
            newhead.next=mergeTwoLists(l2.next,l1);
        }
        return newhead;
    }
    var a=new ListNode(1);
    var b=new ListNode(2);
    margelist(a,b);
    mergeKLists(a);
    
    var mergeKLists = function(lists) {
    if(lists.length == 0) return null;
    var k = lists.length;
    while(k > 1){
        for (let i = 0; i < ~~(k / 2); i++) {
            lists[i] = mergeTwoLists(lists[i], lists[i + ~~((k + 1) / 2)]);
        }
        k = ~~((k + 1) / 2);
    }
    return lists[0];
    };
    </script>
</body>
</html>